<template>
  <div class="stat-card" :style="{ borderTopColor: color }">
    <div class="card-icon" :style="{ background: iconBg }">
      <el-icon :size="32" :color="color">
        <component :is="iconComponent" />
      </el-icon>
    </div>
    
    <div class="card-content">
      <div class="card-title">{{ title }}</div>
      <div class="card-value">{{ formattedValue }}</div>
      
      <div v-if="trend" class="card-trend" :class="`trend-${trend}`">
        <el-icon :size="14">
          <component :is="trendIcon" />
        </el-icon>
        <span>{{ trendValue }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import {
  DataAnalysis,
  Calendar,
  TrendCharts,
  PieChart,
  Top,
  Bottom,
  Right
} from '@element-plus/icons-vue'

// Props
const props = defineProps({
  title: {
    type: String,
    required: true
  },
  value: {
    type: [Number, String],
    required: true
  },
  icon: {
    type: String,
    default: 'DataAnalysis'
  },
  trend: {
    type: String,
    validator: (value) => ['up', 'down', 'flat', ''].includes(value),
    default: ''
  },
  trendValue: {
    type: String,
    default: ''
  },
  color: {
    type: String,
    default: '#409EFF'
  }
})

// 图标映射
const iconMap = {
  DataAnalysis,
  Calendar,
  TrendCharts,
  PieChart
}

const iconComponent = computed(() => {
  return iconMap[props.icon] || DataAnalysis
})

const iconBg = computed(() => {
  // 将颜色转换为半透明背景
  return props.color + '15'
})

const formattedValue = computed(() => {
  if (typeof props.value === 'number') {
    return props.value.toLocaleString()
  }
  return props.value
})

const trendIcon = computed(() => {
  const trendIconMap = {
    up: Top,
    down: Bottom,
    flat: Right
  }
  return trendIconMap[props.trend] || Right
})
</script>

<style lang="scss" scoped>
.stat-card {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 24px;
  background: #ffffff;
  border-radius: 12px;
  border-top: 3px solid;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  cursor: pointer;

  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
  }

  .card-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 64px;
    height: 64px;
    border-radius: 12px;
    flex-shrink: 0;
  }

  .card-content {
    flex: 1;
    min-width: 0;

    .card-title {
      font-size: 14px;
      color: #909399;
      margin-bottom: 8px;
    }

    .card-value {
      font-size: 28px;
      font-weight: 600;
      color: #303133;
      margin-bottom: 8px;
      line-height: 1;
    }

    .card-trend {
      display: flex;
      align-items: center;
      gap: 4px;
      font-size: 13px;
      font-weight: 500;

      &.trend-up {
        color: #67C23A;
      }

      &.trend-down {
        color: #F56C6C;
      }

      &.trend-flat {
        color: #909399;
      }
    }
  }
}

// 响应式
@media (max-width: 1200px) {
  .stat-card {
    padding: 20px;

    .card-icon {
      width: 56px;
      height: 56px;

      :deep(.el-icon) {
        font-size: 28px;
      }
    }

    .card-content {
      .card-value {
        font-size: 24px;
      }
    }
  }
}
</style>

